﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using Rung.EBook.Common.Model;
using Rung.EBook.Common.Exceptions;

namespace Rung.EBook.DocFormater.Umd
{
    [Serializable]
    public class UmdBookBM : BookBase
    {
        // Fields
        private new List<ChapterBM> mChapters = new List<ChapterBM>();

        /// <summary>
        /// 验证该书籍信息是否已经完备，并且可以被保存。
        /// </summary>
        public string CanSave()
        {
            if ((this.Title == null) || (this.Title.Length < 1))
            {
                return "标题不能为空！";
            }
            if ((this.Author == null) || (this.Author.Length < 1))
            {
                return "作者不能为空！";
            }
            if ((this.Chapters == null) || (this.Chapters.Count < 1))
            {
                return "内容数量不能小于0！";
            }
            if ((this.FilePath == null) || (this.FilePath.Length < 1))
            {
                return "保存文件的路径不正确！";
            }
            if (File.Exists(this.FilePath))
            {
                try
                {
                    File.Delete(this.FilePath);
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                    return "无法覆盖，文件正在被使用！";
                }
            }
            return null;
        }

        /// <summary>
        /// 书籍格式（文本型、漫画型、连环画型）
        /// </summary>
        public byte[] Formate
        {
            get;
            set;
        }

        /// <summary>
        /// 书籍类别（文艺类、军事类等）
        /// </summary>
        public string Kind
        {
            get;
            set;
        }

        /// <summary>
        /// 发行者
        /// </summary>
        public string Publisher
        {
            get;
            set;
        }

        /// <summary>
        /// 贩卖商
        /// </summary>
        public string Vendor
        {
            get;
            set;
        }

        /// <summary>
        /// 封面
        /// </summary>
        public Image Cover
        {
            get;
            set;
        }

        /// <summary>
        /// 书籍章节集合
        /// </summary>
        public new List<ChapterBM> Chapters
        {
            get
            {
                return this.mChapters;
            }
        }

        public override void LoadByProject(string filePath)
        {
            base.LoadByProject(filePath);

            UmdBookBM bBM = this.mProjectBM.Book as UmdBookBM;

            if (this.mProjectBM == null) throw (new ProjectFormateException("项目文件格式错误（2）。"));

            if (bBM == null)
            {
                this.mChapters.Clear();
                foreach (ChapterBase cBase in this.mProjectBM.Book.Chapters)
                {
                    ChapterBM cBM = new ChapterBM();
                    cBM.FromChapterBase(cBase);

                    this.mChapters.Add(cBM);
                }
            }
            else
            {
                this.mChapters = bBM.mChapters;
                this.Formate = bBM.Formate;
                this.Kind = bBM.Kind;
                this.Publisher = bBM.Publisher;
                this.Vendor = bBM.Vendor;
                this.Cover = bBM.Cover;
            }

        }

        //public override void FromBookBase(BookBase bBase)
        //{
        //    base.Author = bBase.Author;
        //    base.Title = bBase.Title;
        //}

        public override void FromBookBase(BookBase bb)
        {
            this.Author = bb.Author;
            this.Title = bb.Title;
            this.mDate = bb.Date;
            this.mEncoding = bb.Encoding;
            this.FilePath = bb.FilePath;

            this.mChapters = new List<ChapterBM>();
            foreach (var c in bb.Chapters)
            {
                this.Chapters.Add(new ChapterBM
                {
                    Index = c.Index,
                    Url = c.Url,
                    Title = c.Title,
                    Content = c.Content
                });
            }
        }

        public override void AppendChapter(ChapterBase chapter)
        {
            chapter.Index = this.mChapters.Count;
            this.mChapters.Add((ChapterBM)chapter);
        }
    }
}

